<?php
	class RegisterAction extends BaseAction{
			//验证码
		public function verify(){
			import("ORG.Util.Image");
			Image::buildImageVerify();
		}

		public function index(){
			if(!$this->isPost()){
				$this->display();
			}else{
				if($_POST['fun']=="checks"){
					if($_POST['type']=='1'){
						$this->display("broker");
					}elseif($_POST['type']=='2'){
						$this->display("brokercomp");
					}elseif($_POST['type']=='3'){
						$this->display("tenement");
					}elseif($_POST['type']=='4'){
						$this->display("personal");
					}
				
				}elseif($_POST['fun']=="checkusername"){
					$username = $_POST['username'];
					if($username ==''){
						echo "<span class='jinggaok'>姓名不能为空</span>";
					}else{
						if(preg_match("/^[\x{4e00}-\x{9fa5}]+$/u",$username)){
							echo "正确";
						}else{
							echo "请输入正确姓名";
						}

					}
				}elseif($_POST['fun']=='pwd'){
					$_SESSION['pwd'] = $_POST['pwd'];
				}elseif($_POST['fun']=='agpwd'){
					$pwd = $_POST['agpwd'];
					if($pwd == $_SESSION['pwd']){
						echo "<span style='color:green'>正确</span>";
					}else{
						echo "<span class='jinggaok' >两次密码不一致</span>";
					}
				}elseif($_POST['fun']=='email'){
					$email = $_POST['checkEmail'];
					$result = M('vip')->where("v_email='$email'")->find();
					//echo M('vip')->getlastsql();exit;
					if($email==''){
						echo "empty";
					}else{
						if($result){
							echo "has";
						}else{
							echo "ture";
						}
					}
				}elseif($_POST['fun'] =='tels'){
					$tel = $_POST['tel'];
					$result = M("Vip")->where("tel")->find();
					if($tel==''){
						echo "empty";
					}else{
						$pattern = '/^13\d{9}|15[0|1|2|3|5|6|7|8|9]\d{8}|18[0|2|3|5|6|7|8|9]\d{8}|14[5|7]\d{8}$/';
						if(!preg_match($pattern,$tel)){
							echo "flase";
						}else{
							if($result){
								echo "has";
							}else{
								echo "ture";
							}
						}
					}
				}elseif($_POST['fun']=='register'){
					if($_SESSION['verify'] != md5(trim($_POST['verify']))) {
						$this->assign("jumpUrl","__URL__/index");
						$this->assign("waitSecond",3);
						$this->error("验证码不正确！");
					}
					//获得图片
					$pic = $_FILES['img'];
					$pic1 = $_FILES['buinessimg'];
					//获得图的临时路径
					$tmpname = $pic['tmp_name'];
					$tmpname1 = $pic1['tmp_name'];
					//获得图片类型
					$type = $pic['type'];
					$type1 = $pic1['type'];
					$type = explode('/',$type);
					$type1 = explode('/',$type1);
					$type_1 = $type[0];
					$type1_1 = $type1[0];
					//重新给图片起名
					$picture='vips_'.time().rand(1000,9999).".".$type[1];
					$picture1='buiness_'.time().rand(1000,9999).".".$type1[1];
				
					//上传图片时 的报错
					$error=$pic['error'];
					$error1=$pic1['error'];
					//图片上传的路径
					$newname=ROOT_DIR.DS.'Public/Upload/'.$picture;
					$newname1=ROOT_DIR.DS.'Public/Upload/'.$picture1;
					if(is_uploaded_file($tmpname) && $type_1 == 'image' && $error == '0'){
						move_uploaded_file($tmpname,$newname);
					}
					if(is_uploaded_file($tmpname1) && $type1_1 == 'image' && $error1 == '0'){
						move_uploaded_file($tmpname1,$newname1);
					}	
					if($_POST['mibao']==''){
						$data['problem'] = $_POST['diymibao'];
					}else{
						$data['problem'] = $_POST['mibao'];
					}
					$data['company_img'] = $picture1;
					$data['v_name'] = $_POST['username'];
					$data['v_pwd'] = md5($_POST['pwd']);
					$data['v_idcardno'] = $_POST['Jmsfzh1'];
					$data['v_email'] = $_POST['email'];
					$data['v_tel'] = $_POST['tel'];
					$data['answer'] = $_POST['answer'];
					$data['v_type'] = $_POST['type'];	
					

					$data['company'] = $_POST['company'];
					$data['sheng'] = $_POST['sheng'];
					$data['shi'] = $_POST['shi'];
					$data['xian'] = $_POST['xian'];
					$data['v_img'] = $picture;
					$data['time'] = date("Y-m-d H:i:s",time());
					
					$result = M("vip")->data($data)->add();
					if($result){
						$body = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><body>感谢您注册东房地产网!<br><br><font color="red">您的登录名为:</font><a href="#">'.$data["v_email"].'</a><br><br>请马上点击以下注册确认链接，激活您的9i爱优惠帐号！<br><br><a href="http://http://www.bjdfdc.com.cn/index.php/Email/index/?email='.$data['v_email'].'">http://http://www.bjdfdc.com.cn/index.php/Email/index/</a><br><br><font color="red">(该链接在48小时内有效，48小时后需要重新注册)</font><br><br>如果通过点击以上链接无法访问，请将该网址复制并粘贴至新的浏览器窗口中。<br><br>如果您错误地收到了此电子邮件，您无需执行任何操作来取消帐号！此帐号将不会启动。<br><br>东房地产网<br><br>'.date("Y-m-d",time()).'</body></html>;';
						//echo $body;exit;
						$results = $this->mail($data['v_email'],$body);
						if($results){
							$this->redirect('Register/success',array('email'=>$data['v_email']),0,0);
						}
					}
				}
			
			}
		
		}
		public function success(){
			$email = $_GET['email'];
			$this->assign("email",$email);
			$this->display();
		
		}
		public function mail($email,$body) {
	    header('Content-type:text/html;charset=utf-8');
	    vendor("PHPMailer.class#phpmailer"); //从PHPMailer目录导入class.phpmailer.php类文件
	     
	    $mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
	 
		$mail->IsSMTP(); // telling the class to use SMTP

		try {
		  $mail->CharSet ="UTF-8";//设定邮件编码
		  $mail->Host       = "smtp.mail.yahoo.cn"; // SMTP server
		  $mail->SMTPDebug  = 1;                     // 启用SMTP调试 1 = errors  2 =  messages
		  $mail->SMTPAuth   = true;                  // 服务器需要验证
		  $mail->Port       = 25;                    // 设置端口
			// $mail->SMTPSecure = "ssl";     
			/*
			$mail->SMTPSecure = "ssl";                 
			$mail->Host       = "smtp.gmail.com";     
			$mail->Port       = 465;                  
			*/	
		  $mail->Username   = "dfhyzs@yahoo.cn"; //SMTP服务器的用户帐号                          发送的邮箱
		  $mail->Password   = "1370135";        //SMTP服务器的用户密码                    邮箱的密码
		  $mail->AddReplyTo('dfhyzs@yahoo.cn', '回复'); //收件人回复时回复到此邮箱,可以多次执行该方法

		  $mail->AddAddress($email, $email);             //接收 的人的邮箱     接收的姓名
		  $mail->SetFrom('dfhyzs@yahoo.cn', '东房地产');   
		// $mail->AddAttachment('./img/phpmailer.gif');      // 添加附件,如果有多个附件则重复执行该方法
		  $mail->Subject = '来自东房地产网的注册确认邮件';
		
		  $mail->MsgHTML($body);
		
		  $mail->Send();
		  return "Message Sent OK</p>\n";
		} catch (phpmailerException $e) {
		  echo $e->errorMessage();
		  return false; //从PHPMailer捕获异常
		} catch (Exception $e) {
		  echo $e->getMessage(); 
		  return false;
		}

	} 

	
	}

?>